package com.zx.map.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.zx.map.dao.mapper.UserCountMapper;
import com.zx.map.dao.mapper.UserMapper;
import com.zx.map.dao.model.User;
import com.zx.map.dao.model.UserCount;
import com.zx.map.dao.model.UserCountExample;
import com.zx.map.dao.model.UserExample;
import com.zx.map.service.UserCountService;
import com.zx.map.service.model.OrdLogModel;

@Component("userCountService")
public class UserCountServiceImpl implements UserCountService {
	@Autowired
	private UserCountMapper mapper;
	
	@Autowired
	private UserMapper umapper;

	@Override
	public List<UserCount> list(User user) {

		UserCountExample example = new UserCountExample();
		UserCountExample.Criteria criteria = example.createCriteria();
		criteria.andShopIdEqualTo(user.getMerchantid());
		example.setOrderByClause(user.getOrderby());
		example.setOrderByDesc(user.getOrderType());
		List<UserCount> ulist = mapper.getPageByModel(user.getPageSize(), user.getPageNum(), example);
		return ulist;
	}

	
	@Override
	public List<UserCount> historylist(User user,Date begin ,Date end) {

		UserCountExample example = new UserCountExample();
		UserCountExample.Criteria criteria = example.createCriteria();
		criteria.andShopIdEqualTo(user.getMerchantid());
		if(begin!=null && end !=null) {
			criteria.andLastTimeBetween(begin, end);
		}
		List<UserCount> ulist = mapper.selectByExample(example);
		return ulist;
	}
	
	@Override
	public List<UserCount> allUserlist(User user) {

		UserCountExample example = new UserCountExample();
		UserCountExample.Criteria criteria = example.createCriteria();
		criteria.andShopIdEqualTo(user.getMerchantid());
		List<UserCount> ulist = mapper.selectByExample(example);
		return ulist;
	}
	
	
	@Override
	public int update(OrdLogModel tempOrd) {
		try {
			// TODO Auto-generated method stub
			
		
			
			String mid =tempOrd.getUserId();
			
			UserExample userExample = new UserExample();
			UserExample.Criteria ucriteria = userExample.createCriteria();
			ucriteria.andMerchantidEqualTo(tempOrd.getMerchantId());
			ucriteria.andIdEqualTo(mid);
			
			List<User> userslist = umapper.selectByExample(userExample);
			User user=null;
			if(userslist==null || userslist.size()==0) {
				 user = new User();
				user.setCreatetime(new Date());
				user.setName(tempOrd.getUserName());
				user.setId(mid);
				user.setMerchantid(tempOrd.getMerchantId());
				umapper.insert(user);
			}else {
				user=userslist.get(0);
			}
			
			
			
			
			UserCountExample example= new UserCountExample();
			UserCountExample.Criteria criteria = example.createCriteria();
			criteria.andUserIdEqualTo(mid);
			criteria.andShopIdEqualTo(tempOrd.getMerchantId());
			List<UserCount> ulist = mapper.selectByExample(example);
			
			
			UserCount uc;
			if(ulist==null || ulist.size()==0) {
				uc = new UserCount();
				uc.setCountNum(1);
				uc.setSumAmt(BigDecimal.valueOf(tempOrd.getPaymentAmt()));
				uc.setLastTime(new Date());
				uc.setUserId(mid);
				uc.setName(tempOrd.getUserName());
				uc.setShopId(tempOrd.getMerchantId());
				mapper.insert(uc);
			}else {
				uc = ulist.get(0);
				uc.setCountNum(uc.getCountNum()+1);
				uc.setSumAmt(BigDecimal.valueOf(tempOrd.getPaymentAmt()).add(uc.getSumAmt()));
				uc.setLastTime(new Date());
				mapper.updateByPrimaryKey(uc);
			}
			
		}catch(Exception e) {
			e.printStackTrace();
		}
	
		return 0;
	}

}
